const mysql = require("mysql")

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'cashbook'
})

connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack)
    return
  }

  console.log('connected as id ' + connection.threadId)
})

let Query = (sql) => {
  return new Promise(
    (resolve, reject) => {
      connection.query(sql, (err, res) => {
        if(err) reject(err)
        else resolve(res)
      })
    }
  )
}

module.exports = Query
